package com.linewow.xhyy.nihaonew.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.linewow.xhyy.nihaonew.application.NiHaoApp;
import com.linewow.xhyy.nihaonew.entity.tag.CustomTagsInfo;
import com.linewow.xhyy.nihaonew.entity.tag.TagInfo;
import com.linewow.xhyy.nihaonew.entity.tag.TagListInfo;
import com.linewow.xhyy.nihaonew.util.CommonUtil;

/**
 * Created by LXR on 2016/9/7.
 */
public class ColorfulTag extends RelativeLayout {
    private LayoutInflater layoutInflater;
    private TextView tv;
    private String TAG="ColorfulTag";
    private Context context;
    private int marginLeft,marginTop,round,stroke;

    public ColorfulTag(Context context) {
        super(context);
        init(context,null);
    }

    public ColorfulTag(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    public ColorfulTag(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        this.context=context;
        marginLeft=CommonUtil.dp2px(context,6);
        marginTop=CommonUtil.dp2px(context,2);
        round=CommonUtil.dp2px(context,15);
        stroke=CommonUtil.dp2px(context,1);
        layoutInflater= LayoutInflater.from(context);
        tv=new TextView(context);
        tv.setTextSize(12);//设置成12 合适其他可能会有问题
        RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.setMargins(marginLeft,marginTop,marginLeft,marginTop);
        tv.setLayoutParams(lp);
        addView(tv);
    }

    public void setContent(TagInfo info){

        tv.setText(info.tag);
        if(info.color.startsWith("#")){
            tv.setTextColor(Color.parseColor(info.color));
            RoundRectShape rs = new RoundRectShape(new float[] { round, round, round,round,round,round,round,round }, null, null);
            ShapeDrawable sdOff = new CustomShapeDrawable(rs, Color.WHITE, Color.parseColor(info.color),stroke);
            StateListDrawable stld = new StateListDrawable();
            stld.addState(new int[] { android.R.attr.state_enabled }, sdOff);
            setBackground(stld);
        }
        setTag(info);
    }

    public void setCustomTagsInfo(CustomTagsInfo info){
        setHei(CommonUtil.dp2px(getContext(),20));
        if(NiHaoApp.EnglishFlag){
            tv.setText(info.en);
        }else{
            tv.setText(info.zhcn);
        }
        if(info.color==null){
            info.setColor("#FB586D");
        }
        if(!info.color.startsWith("#")){
            info.setColor("#FB586D");
        }
        if(info.color.startsWith("#")){
            try {
                Color.parseColor(info.color);
            }catch (NumberFormatException e){
                info.setColor("#FB586D");
            }
            tv.setTextColor(Color.parseColor(info.color));
            RoundRectShape rs = new RoundRectShape(new float[] { round, round, round,round,round,round,round,round }, null, null);
            ShapeDrawable sdOff = new CustomShapeDrawable(rs, Color.WHITE, Color.parseColor(info.color),stroke);
            StateListDrawable stld = new StateListDrawable();
            stld.addState(new int[] { android.R.attr.state_enabled }, sdOff);
            setBackground(stld);
        }
        setTag(info);
    }



    public void setCustomTagsInfo(TagListInfo info){
        if(NiHaoApp.EnglishFlag){
            tv.setText(info.en);
        }else{
            tv.setText(info.zhcn);
        }
        if(info.color==null){
            info.setColor("#FB586D");
        }
        if(!info.color.startsWith("#")){
            info.setColor("#FB586D");
        }
        if(info.color.startsWith("#")){
            try {
                Color.parseColor(info.color);
            }catch (NumberFormatException e){
                info.setColor("#FB586D");
            }
            tv.setTextColor(Color.parseColor(info.color));
            RoundRectShape rs = new RoundRectShape(new float[] { round, round, round,round,round,round,round,round }, null, null);
            ShapeDrawable sdOff = new CustomShapeDrawable(rs, Color.WHITE, Color.parseColor(info.color),stroke);
            StateListDrawable stld = new StateListDrawable();
            stld.addState(new int[] { android.R.attr.state_enabled }, sdOff);
            setBackground(stld);
        }
        setTag(info);
    }

    public void setHei(int p){
        tv.setHeight(p);
    }


}